from flask import request,make_response

from info import redis_store
from info.utils.captcha.captcha import captcha
from .import passport_blue



@passport_blue.route('/image_code')
def image_code():
    #1.获取参数cur_id
    cur_id = request.args.get("cur_id")
    pre_id = request.args.get("pre_id")
    #2.校验参数
    if not cur_id:
        return "参数不全"
    if pre_id:
        redis_store.delete("image_code:%s" % pre_id)

    #3.生成验证码
    name ,text,image_code = captcha.generate_captcha()
    #4.储存到redis里面
    redis_store.set("image_code:%s"%cur_id,text,300)
    #5.返回图片验证码
    response = make_response(image_code)
    response.headers["Content-Type"] = "image/png"
    return response